package com.rise.remi.world;

import com.rise.remi.core.Global;
import com.rise.remi.layer.TiledLayer;
import com.rise.remi.player.Player;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author remi
 */
public class MapGenerator {

    private static Random rnd = new Random();
    private static BufferedImage miniMap;
    private static Color light = new Color(192, 192, 192, 255), dark = new Color(128, 128, 128, 255);

    public static TiledLayer generate_v2(TiledLayer layer, World world, Player player, ArrayList<Room> rooms) {
        //заполнение пола рандомными тайлами
        for (int i = 0; i < Global.horizontalTiles; i++) {
            for (int j = 0; j < Global.verticalTiles; j++) {
                int tile = rnd.nextInt(2);
                switch (tile) {
                    case 0:
                        layer.setTile(i, j, 0);
                        break;
                    case 1:
                        layer.setTile(i, j, 18);
                        break;
                }
            }
        }

        layer.setTile(0, 0, 1); //стена с кружком
        layer.setTile(1, 0, 10); //стена из решетки с голубой обводкой
        layer.setTile(2, 0, 2); //дверь
        layer.setTile(3, 0, 10);
        layer.setTile(4, 0, 16); //стена лево гориз
        layer.setTile(5, 0, 17); //стена право гориз

        layer.setTile(0, 0, 1);
        layer.setTile(0, 1, 1);
        layer.setTile(0, 2, 10);
        layer.setTile(0, 3, 2);
        layer.setTile(0, 4, 1);
        layer.setTile(0, 5, 1);

        layer.setTile(5, 1, 8);
        layer.setTile(5, 2, 9);
        layer.setTile(5, 3, 11);
        layer.setTile(5, 4, 12);
        layer.setTile(5, 5, 13);

        layer.setTile(1, 5, 17);
        layer.setTile(2, 5, 17);
        layer.setTile(3, 5, 2);
        layer.setTile(4, 5, 17);


        layer.setTile(8, 8, 7);
        layer.setTile(9, 8, 7);

        layer.setTile(8, 10, 7);
        layer.setTile(9, 10, 7);

        layer.setTile(10, 6, 7);
        layer.setTile(11, 7, 7);
        layer.setTile(11, 8, 7);
        layer.setTile(11, 9, 7);
        layer.setTile(11, 10, 7);
        layer.setTile(11, 11, 7);
        layer.setTile(10, 12, 7);
        
        layer.setTile(15, 15, 22);




//        for (int j = 0; j < 1; j++) {
//            int roomX, roomY;
//            roomX = rnd.nextInt(40);
//            roomY = rnd.nextInt(40);
//            int roomW, roomH;
//            roomW = rnd.nextInt(10) + 5;
//            roomH = rnd.nextInt(10) + 5;
//
//            if (!isThereRoom(world, roomX, roomY, roomW, roomH)) {
//                Room room = new Room(j, roomX, roomY, roomW, roomH);
//                drawRoom(layer, roomX, roomY, roomW, roomH, 14);
//                drawTonnel(world, layer, room);
//            }
//
//        }

        writeMiniMap(world, player);
        return layer;

    }

    public static TiledLayer generate(TiledLayer layer, World world, Player player) {
        for (int i = 0; i < Global.horizontalTiles; i++) {
            for (int j = 0; j < Global.verticalTiles; j++) {
                //layer.setTile(i, j, rnd.nextInt(9));
            }
        }
        //
        for (int i = 0; i < Global.horizontalTiles; i++) {
            for (int j = 0; j < Global.verticalTiles; j++) {
                //layer.setTile(i, j, rnd.nextInt(9) + 8);
            }
        }
        //test room
        //for (int i = 0; i < 25; i++) {
        //drawRoom(layer, rnd.nextInt(45), rnd.nextInt(45), rnd.nextInt(10) + 5, rnd.nextInt(10) + 5, 14);
        drawRoom(layer, 0, 0, Global.horizontalTiles - 1, Global.verticalTiles - 1, Global.obstacles[0]);

        for (int i = 0; i < 150; i++) {
            //vert
            int x = rnd.nextInt(64);
            int y = rnd.nextInt(63);

            fillRoom(layer, x, y, 1, rnd.nextInt(64 - y), 0);
            //hor
            fillRoom(layer, x, y, rnd.nextInt(64 - x), 1, 0);            //fillRoom(layer, rnd.nextInt(43), rnd.nextInt(43), rnd.nextInt(20), rnd.nextInt(20), 0);
            //fillRoom(layer, 5 + rnd.nextInt(Global.horizontalTiles - 10), 0, 1, Global.verticalTiles, Global.obstacles[0]);
            //fillRoom(layer, 0, 5 + rnd.nextInt(Global.verticalTiles - 10), Global.horizontalTiles, 1, Global.obstacles[0]);
        }

        writeMiniMap(world, player);

        return layer;

    }

    private static void fillRoom(TiledLayer layer, int x, int y, int w, int h, int tileId) {
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                layer.setTile(i + x, j + y, tileId);
            }
        }
    }

    private static void drawRoom(TiledLayer layer, int x, int y, int w, int h, int tileId) {

        for (int i = 0; i < w; i++) {
            int random = rnd.nextInt(3);
            layer.setTile(i + x, y, tileId + random);
            layer.setTile(i + x, y + h, tileId + random);
        }
        for (int j = 0; j < h + 1; j++) {
            int random = rnd.nextInt(3);
            layer.setTile(x, y + j, tileId + random);
            layer.setTile(w + x, y + j, tileId + random);
        }
        //doors vert
        //layer.setTile(x + (w / 2), y, 0);
        //layer.setTile(x + (w / 2), y + h, 0);
        //doors horiz
        //layer.setTile(x, y + (h / 2), 0);
        //layer.setTile(x + w, y + (h / 2), 0);
    }

    public static void writeMiniMap(World world, Player player) {
        miniMap = new BufferedImage(Global.horizontalTiles * 3, Global.verticalTiles * 3, BufferedImage.TYPE_INT_ARGB);
        Graphics g = miniMap.getGraphics();
        for (int i = 0; i < Global.horizontalTiles; i++) {
            for (int j = 0; j < Global.verticalTiles; j++) {
                if (!CheckObstacles.checkObstacle(world.getWorldLayer().getTile(i, j))) {
                    g.setColor(light);
                } else {
                    g.setColor(dark);
                }
                g.fillRect(i * 3, j * 3, 3, 3);
            }
        }

        //miniMap.flush();
        //end method
    }

    private static boolean isThereRoom(World world, int x, int y, int w, int h) {
        int errors = 0;
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if (CheckObstacles.checkObstacle(world.getWorldLayer().getTile(i + x, j + y))) {
                    errors++;
                }
            }
        }
        if (errors == 0) {
            return false;
        } else {
            return true;
        }
    }

    private static void drawTonnel(World world, TiledLayer layer, Room room) {
        int direction = rnd.nextInt(2);
        int h, offs;

        switch (direction) {

            case 0: //up
                h = rnd.nextInt(5) + 5;
                offs = rnd.nextInt(room.getW() - 2);

                drawRoom(layer, room.getX() + offs + 1, room.getY(), 0, 0, 0);
                drawRoom(layer, room.getX() + offs, room.getY() - h, 0, h, 14);
                drawRoom(layer, room.getX() + offs + 2, room.getY() - h, 0, h, 14);
                break;
            case 1: //down
                h = rnd.nextInt(5) + 5;
                offs = rnd.nextInt(room.getW() - 2);

                drawRoom(layer, room.getX() + offs + 1, room.getY() + room.getH(), 0, 0, 0);
                drawRoom(layer, room.getX() + offs, room.getY() + room.getH(), 0, h, 14);
                drawRoom(layer, room.getX() + offs + 2, room.getY() + room.getH(), 0, h, 14);
                break;
            case 2: //left
                break;
            case 3: //right
                break;
        }
    }

    public static BufferedImage getMiniMap() {
        return miniMap;
    }
}
